<?php
namespace app\admin\controller;
use app\admin\model\{Artist, Goods as GoodsModel,StoreCategory,GoodsAttr,ProductRule,GoodsAttrResult,GoodsAttrValue, MerchantWithdrawApply, UserMerchant};
use think\facade\Db;
use app\common\BaseController;

class Merchant extends BaseController {

    public function list(){
        $page  = (int)request()->param('page/d', 0);
        $limit  = (int)request()->param('limit/d', 10);
        $search = request()->param();
        $where = [];
        if(isset($search["searchword"]) && $search["searchword"]!="") $where[] = ["shop_name|phone|mobile","like","%{$search["searchword"]}%"];
        $query  = UserMerchant::where($where);
        $total  = $query->count();
        $data   = $query->page($page,$limit)->order('id desc')->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total,
        ]);
    }

    //提现列表
    public function withdrawal_list(){
        $page  = (int)request()->param('page/d', 0);
        $limit  = (int)request()->param('limit/d', 10);
        $search = request()->param();
        $where = [];
        if(isset($search["searchword"]) && $search["searchword"]!="") $where[] = ["enc_bank_no|enc_true_name|bank_code","like","%{$search["searchword"]}%"];
        if(isset($search["merchant_id"]) && $search["merchant_id"]!="") $where[] = ["merchant_id","=",$search["merchant_id"]];
        if(isset($search["status"]) && $search["status"]!="") $where[] = ["status","=",$search["status"]];
        $query  = MerchantWithdrawApply::with(['merchant'])->where($where)->append(['status_text','bank_name']);
        $total  = $query->count();
        $data   = $query->page($page,$limit)->order('id desc')->select();
        $merchant_list = UserMerchant::where('is_del',0)->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total,
            'merchant_list' => $merchant_list
        ]);
    }

    //详情
    public function detail(){
        $id  = (int)request()->param('id/d', 0);
        $detail = UserMerchant::find($id);
        return $this->success('', $detail);
    }


         /**
     * 审核
     * @return [type] [description]
     */
    public function approve(){
        $id  = param_check('id');
        try {
            $goods = UserMerchant::find($id);
            if(!$goods){
                exception('商家不存在');
            }
            $goods->status = 1;
            $goods->save();
        } catch (\Exception $e) {
            json_response(0,$e->getMessage());
        }
        json_response(1,'编辑成功');
    }

    /**
     * 拒绝
     * @return [type] [description]
     */
    public function reject(){
        $id  = param_check('id');
        try {
            $data = request()->post();
            if(empty($data['id'])) exception('请传入ID');
            if(empty($data['reason'])) exception('请传入拒绝理由');
            $goods = UserMerchant::find($id);
            if(!$goods){
                exception('商家不存在');
            }
            $goods->status = 2;
            $goods->fail_msg = $data['reason'];
            $goods->save();
        } catch (\Exception $e) {
            json_response(0,$e->getMessage());
        }
        json_response(1,'编辑成功');
    }



             /**
     * 审核
     * @return [type] [description]
     */
    public function withdrawal_approve(){
        $id  = param_check('id');
        try {
            $goods = MerchantWithdrawApply::find($id);
            if(!$goods){
                exception('记录不存在');
            }
            $goods->status = 1;
            $goods->save();
        } catch (\Exception $e) {
            json_response(0,$e->getMessage());
        }
        json_response(1,'编辑成功');
    }

    /**
     * 拒绝
     * @return [type] [description]
     */
    public function withdrawal_reject(){
        $id  = param_check('id');
        Db::startTrans();
        try {
            $data = request()->post();
            if(empty($data['id'])) exception('请传入ID');
            if(empty($data['reason'])) exception('请传入拒绝理由');
            $info = MerchantWithdrawApply::find($id);
            if(!$info){
                exception('记录不存在');
            }
            $merchant = UserMerchant::where('id',$info->merchant_id)->lock(true)->find();
            if(!$merchant){
                exception('商家不存在');
            }
            $info->status = 2;
            $info->fail_msg = $data['reason'];
            $info->save();
            $merchant->balance += $info->amount;
            $merchant->save();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            json_response(0,$e->getMessage());
        }
        json_response(1,'编辑成功');
    }


          /**
     * 删除
     * @return [type] [description]
     */
    public function delete(){
        $id  = param_check('ids');
        try {
            $goods = UserMerchant::find($id);
            if(!$goods){
                exception('商家不存在');
            }
            if($goods->is_del == 0){
                $goods->is_del = 1;
            } else{
                $goods->is_del = 0;
            }
           
            $goods->save();
        } catch (\Exception $e) {
            json_response(0,$e->getMessage());
        }
        json_response(1,'编辑成功');
    }
}
